﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Utility;
using WPay.Infrastructure.Unity;

namespace CommonUtility
{
    public static class LoggerHelper
    {
        private const string DirectPath = "Log";
        private const string BrStr = "---------------------------------------------------------------------------";
        private static Queue<string> ListQueue = new Queue<string>();
        public static object _lock = new object();
        public static void Log(string msg)
        {
            Writelog(msg);
            //ListQueue.Enqueue(msg);
        }

        public static void UseLocalLog(this IServiceCollection services)
        {
            Writelog($"Project start!,当前版本v_2024-02-02-{DateTime.Now.ToString("yyyyMMMdd-HH-mm-ss")},当前环境(0-production 1-development):{ConfigManage.GetConfigByKey("IsDev")}");
#if !DEBUG
                      //Email.SendEmail("jenkins项目构建", new List<string>() { "xilianxinlingshou@wumis.com" }, $"[{AppDomain.CurrentDomain.FriendlyName}]项目已启动!,当前环境(0-生产 1-开发):{ConfigManage.GetConfigByKey("IsDev")}", new List<string>() { "xilianxinlingshou@wumis.com" }, true, null,"西点云支付");
#endif

        }
        public static void Writelog(string msg)
        {
            lock (_lock)
            {

                var dt = DateTime.Now;
                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/" + DirectPath);
                if (di.Exists == false) di.Create();
                DirectoryInfo disub = new DirectoryInfo(di.ToString() + "/" + $"{AppDomain.CurrentDomain.FriendlyName}");
                if (disub.Exists == false) disub.Create();
                var FilePath = disub.ToString() + "/" + dt.ToString("yyyy-MM-dd") + "-" + IPHelper.DockerId() + ".txt";
                if (!File.Exists(FilePath))
                {
                    using (var fs = File.Create(FilePath))
                    {
                        fs.Flush();
                    }
                }
                using (
                    var filestream = new System.IO.FileStream(FilePath, System.IO.FileMode.Append,
                        System.IO.FileAccess.Write, FileShare.ReadWrite))
                {
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    using (var sw = new StreamWriter(filestream, Encoding.GetEncoding("gb2312")))

                    {
                        Console.WriteLine($"[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" + msg);
                        // sw.WriteLine($"[" + dt.ToString("yyyy-MM-dd HH:mm:ss:fff") + "]" + msg);
                        sw.WriteLine(msg);
                        sw.Flush();
                    }
                }


            }
        }
        public static void Writelog2(string msg)
        {

            lock (_lock)
            {


                var dt = DateTime.Now;
                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/" + DirectPath);
                if (di.Exists == false) di.Create();
                DirectoryInfo disub = new DirectoryInfo(di.ToString() + "/" + $"{AppDomain.CurrentDomain.FriendlyName}");
                if (disub.Exists == false) disub.Create();
                var FilePath = disub.ToString() + "/" + dt.ToString("yyyy-MM-dd") + ".txt";
                if (!File.Exists(FilePath))
                {
                    using (var fs = File.Create(FilePath))
                    {
                        fs.Flush();
                    }
                }
                using (
                    var filestream = new System.IO.FileStream(FilePath, System.IO.FileMode.Append,
                        System.IO.FileAccess.Write, FileShare.ReadWrite))
                {
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    using (var sw = new StreamWriter(filestream, Encoding.GetEncoding("gb2312")))

                    {
                        Console.WriteLine(msg);
                        sw.WriteLine(msg);
                        //sw.WriteLine(BrStr);
                        sw.Flush();
                    }
                }


            }
        }
        public static void Writelog(string filename, string msg)
        {
            lock (_lock)
            {

                var dt = DateTime.Now;
                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/" + DirectPath);
                if (di.Exists == false) di.Create();
                DirectoryInfo disub = new DirectoryInfo(di.ToString() + "/" + $"{AppDomain.CurrentDomain.FriendlyName}");
                if (disub.Exists == false) disub.Create();
                var FilePath = disub.ToString() + "/" + filename + dt.ToString("yyyy-MM-dd") + ".txt";
                if (!File.Exists(FilePath))
                {
                    using (var fs = File.Create(FilePath))
                    {
                        fs.Flush();
                    }
                }
                using (
                    var filestream = new System.IO.FileStream(FilePath, System.IO.FileMode.Append,
                        System.IO.FileAccess.Write, FileShare.ReadWrite))
                {
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    using (var sw = new StreamWriter(filestream, Encoding.GetEncoding("gb2312")))

                    {
                        Console.WriteLine($"[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" + msg);
                        sw.WriteLine($"[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" + msg);
                        sw.Flush();
                    }
                }



            }
        }
        public static void Log(string source, string msg)
        {
            lock (_lock)
            {

                var dt = DateTime.Now;
                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/" + DirectPath);
                if (di.Exists == false) di.Create();
                DirectoryInfo disub = new DirectoryInfo(di.ToString() + "/" + $"{AppDomain.CurrentDomain.FriendlyName}");
                if (disub.Exists == false) disub.Create();
                var FilePath = disub.ToString() + "/" + dt.ToString("yyyy-MM-dd") + "-" + IPHelper.DockerId() + ".txt";
                if (!File.Exists(FilePath))
                {
                    using (var fs = File.Create(FilePath))
                    {
                        fs.Flush();
                    }
                }
                using (
                    var filestream = new System.IO.FileStream(FilePath, System.IO.FileMode.Append,
                        System.IO.FileAccess.Write, FileShare.ReadWrite))
                {

                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    using (var sw = new StreamWriter(filestream, Encoding.GetEncoding("gb2312")))

                    {
                        Console.WriteLine(source + "[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" + msg);
                        sw.WriteLine("[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" + source + msg);
                        sw.Flush();
                    }
                }


            }
        }
    }
}
